#!/usr/bin/python
# -*- coding: UTF-8 -*-
import rospy
from std_msgs.msg import String
from robot_by_fe3.msg import location
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QFileDialog,QMessageBox
from Robot_Fe_OS import Ui_Robot_Fe_OS
import pandas as pd
import os
import time

class main_ui(QWidget,Ui_Robot_Fe_OS):
    def __init__(self):  # 这是初始化函数一进来就会自动执行
        super(main_ui, self).__init__()
        self.setupUi(self)
        self.cwd = os.getcwd()  # 获取当前程序文件位置
        self.comboBox.addItem('0')
        self.pushButton_action.clicked.connect(lambda :self.robot_action())
        self.pushButton_read_path.clicked.connect(lambda :self.read_excel())
        self.pushButton_add_path.clicked.connect(lambda :self.send_QR_code())
        self.pushButton_stop.clicked.connect(lambda :self.robot_stop())
        self.location_label=[]
        self.next_location_label=[]
        self.direction=[]
        # self.i=0
        rospy.init_node('Fe_Master', anonymous=True)
        self.location_data_pub = rospy.Publisher('location', location, queue_size=10)
        self.robot_action_pub= rospy.Publisher('fe_robot_action', String, queue_size=10)
        self.robot_stop_pub=rospy.Publisher('fe_robot_stop', String, queue_size=10)
        rospy.Subscriber('robot_state', String, self.robot_state)


    def read_excel(self):
        fileName_choose, filetype = QFileDialog.getOpenFileName(self, "选择文件", self.cwd, "XLSX (*.xlsx)")
        if fileName_choose == "":
            #QMessageBox.about(self,"a","a")
            return
        else:
            #print(fileName_choose)
            data = pd.DataFrame(pd.read_excel(fileName_choose))  # 读取数据,设置None可以生成一个字典，字典中的key值即为sheet名字，此时不用使用DataFram，会报错
            #print(data.index)  # 获取行的索引名称
        # print(data.columns)#获取列的索引名称
            self.location_label = data['location_label']
            self.next_location_label=data['next_location_label']
            self.direction=data['direction']
            print(len(self.location_label))
            print(self.location_label)
            self.label_path.setText(fileName_choose)
            QMessageBox.about(self, "提醒", "路线数据载入成功！")
        # if str(a[0]) == 'A-01':
        #     print("ok")
        pass
    def send_QR_code(self):
        data = location()
        data.location_label = 'location_label'
        data.next_location_label = 'next_location_label'
        data.direction = 'direction'
        self.location_data_pub.publish(data)
        time.sleep(0.1)
        for i in range(len(self.location_label)):
            data.location_label=self.location_label[i]
            data.next_location_label=self.next_location_label[i]
            data.direction=self.direction[i]
            self.location_data_pub.publish(data)
            time.sleep(0.1)

        pass
    def robot_action(self):
        self.robot_action_pub.publish('ACTION')
        # pub.publish(hello_str)
        pass
    def robot_stop(self):
        self.robot_action_pub.publish('STOP')
        # pub.publish(hello_str)
        pass
    def robot_state(self,data):
        s='当前机器人位于'+str(data)+'区'
        self.label_3.setText(s)
        #print data
        #pass


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui=main_ui()
    ui.show()
    sys.exit(app.exec_())
